package com.youlai.boot.food.controller;

import com.youlai.boot.food.service.SysFoodInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.food.model.form.SysFoodInfoForm;
import com.youlai.boot.food.model.query.SysFoodInfoQuery;
import com.youlai.boot.food.model.vo.SysFoodInfoVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 食物数据前端控制层
 *
 * @author cuisheng
 * @since 2025-03-30 16:17
 */
@Tag(name = "食物数据接口")
@RestController
@RequestMapping("/api/v1/sysFoodInfos")
@RequiredArgsConstructor
public class SysFoodInfoController  {

    private final SysFoodInfoService sysFoodInfoService;

    @Operation(summary = "食物数据分页列表")
    @GetMapping("/page")
    public PageResult<SysFoodInfoVO> getSysFoodInfoPage(SysFoodInfoQuery queryParams ) {
        IPage<SysFoodInfoVO> result = sysFoodInfoService.getSysFoodInfoPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增食物数据")
    @PostMapping
    public Result<Void> saveSysFoodInfo(@RequestBody @Valid SysFoodInfoForm formData ) {
        boolean result = sysFoodInfoService.saveSysFoodInfo(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取食物数据表单数据")
    @GetMapping("/{id}/form")
    public Result<SysFoodInfoForm> getSysFoodInfoForm(
        @Parameter(description = "食物数据ID") @PathVariable Long id
    ) {
        SysFoodInfoForm formData = sysFoodInfoService.getSysFoodInfoFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改食物数据")
    @PutMapping(value = "/{id}")
    public Result<Void> updateSysFoodInfo(
            @Parameter(description = "食物数据ID") @PathVariable Long id,
            @RequestBody @Validated SysFoodInfoForm formData
    ) {
        boolean result = sysFoodInfoService.updateSysFoodInfo(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除食物数据")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteSysFoodInfos(
        @Parameter(description = "食物数据ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = sysFoodInfoService.deleteSysFoodInfos(ids);
        return Result.judge(result);
    }
}
